package com.cucn.ingmall.order.listener;

import com.cucn.common.constant.order.OrderRabbitMqConstant;
import com.cucn.common.constant.seckill.SeckillConstant;
import com.cucn.common.to.mq.SeckillOrderTO;
import com.cucn.ingmall.order.entity.OrderEntity;
import com.cucn.ingmall.order.service.OrderService;
import com.rabbitmq.client.Channel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.Date;

/**
 * @author KouChaoJie
 * @since: 2022/2/22 14:26
 */
@Component
@Slf4j
@RabbitListener(queues = SeckillConstant.ORDER_SECKILL_QUEUE)
public class OrderSeckillListener {
    @Resource
    private OrderService orderService;

    @RabbitListener(queues = SeckillConstant.ORDER_SECKILL_QUEUE)
    public void listener(SeckillOrderTO seckillOrderTO, Channel channel, Message message) throws IOException {
        log.info("收到秒杀订单请求,准备创建秒杀单的详细信息:{},发送时间:{}", seckillOrderTO.getOrderSn(), new Date());

        log.info("准备创建秒杀单的详细信息...");

        try {
            orderService.createSeckillOrder(seckillOrderTO);
            channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
        } catch (Exception e) {
            channel.basicReject(message.getMessageProperties().getDeliveryTag(),true);
        }
    }
}
